﻿using AutoMapper;
using Compass.LogisticService.Domain;
using Compass.LogisticService.Domain.Entities;
using Compass.LogisticService.Infrastructure;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Categories;
using Compass.Wasm.Shared.Logistics;

namespace Compass.Wasm.Server.Services.Logistics;
public interface IImportExportInfoService:IBaseService<ImportExportInfoDto>
{

}
public class ImportExportInfoService:IImportExportInfoService
{
    #region ctor
    private readonly LogisticDbContext _dbContext;
    private readonly ILogisticRepository _repository;
    private readonly IMapper _mapper;
    public ImportExportInfoService(LogisticDbContext dbContext,ILogisticRepository repository,IMapper mapper)
    {
        _dbContext = dbContext;
        _repository = repository;
        _mapper = mapper;
    }
    #endregion

    #region 基本增删改查
    public async Task<ApiResponse<IEnumerable<ImportExportInfoDto>>> GetAllAsync()
    {
        try
        {
            var models = await _repository.GetImportExportInfosAsync();
            var dtos = await _mapper.ProjectTo<ImportExportInfoDto>(models).OrderBy(x=>x.MtlNumber).ToListAsync();
            return new ApiResponse<IEnumerable<ImportExportInfoDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<ImportExportInfoDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<ImportExportInfoDto>> GetSingleAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetImportExportInfoByIdAsync(id);
            if (model == null) return new ApiResponse<ImportExportInfoDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<ImportExportInfoDto>(model);
            return new ApiResponse<ImportExportInfoDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<ImportExportInfoDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<ImportExportInfoDto>> AddAsync(ImportExportInfoDto dto)
    {
        try
        {
            var model = new ImportExportInfo(Guid.NewGuid(), dto.MtlNumber, dto.Type,dto.Sbu,
                dto.Name,dto.HsCode,dto.HsDescription,dto.PlaceOfOrigin,dto.DeclarationElements,
                dto.NetWeight,dto.PoNumbers, dto.NavDescription, 
                dto.AttachmentsUrl,dto.ImagesUrl);
            await _dbContext.ImportExportInfos.AddAsync(model);
            dto.Id = model.Id;
            return new ApiResponse<ImportExportInfoDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<ImportExportInfoDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<ImportExportInfoDto>> UpdateAsync(Guid id, ImportExportInfoDto dto)
    {
        try
        {
            var model = await _repository.GetImportExportInfoByIdAsync(id);
            if (model == null) return new ApiResponse<ImportExportInfoDto> { Status = false, Message = "更新数据失败" };
            model.Update(dto);
            return new ApiResponse<ImportExportInfoDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<ImportExportInfoDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<ImportExportInfoDto>> DeleteAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetImportExportInfoByIdAsync(id);
            if (model == null) return new ApiResponse<ImportExportInfoDto> { Status = false, Message = "删除数据失败" };
            model.SoftDelete();//软删除
            return new ApiResponse<ImportExportInfoDto> { Status = true };
        }
        catch (Exception e)
        {
            return new ApiResponse<ImportExportInfoDto> { Status = false, Message = e.Message };
        }
    } 
    #endregion
}